#!/usr/bin/env python
"""A moving target."""

from Feedback import Feedback
import time
import VisionEgg
VisionEgg.start_default_logging(); VisionEgg.watch_exceptions()
from VisionEgg.Core import *
from VisionEgg.Text import *
from VisionEgg.FlowControl import Presentation, Controller, FunctionController
from VisionEgg.MoreStimuli import *
from math import *

class FeedbackSkeleton(Feedback):

################################################################################
# From Feedback Base Class
################################################################################
    def on_init(self):
        self.stopping, self.stopped, self.paused = False, False, False
        
        
        # Initialize OpenGL graphics screen.
        self.screen = VisionEgg.Core.get_default_screen()
        self.screen.parameters.bgcolor = (0.0,0.0,0.0,0.0,1.0)
        
        self.mid_x = self.screen.size[0]/2.0
        self.mid_y = self.screen.size[1]/2.0
        self.max_vel = min(self.screen.size[0],self.screen.size[1]) * 0.4
        # Create an instance of the Text class with appropriate parameters.
        # TODO: Find out what works with Text
        self.text = VisionEgg.Text.Text(text="A B C D E F \n G H J K L M \n N O P Q R S \n T U V W X Y \n Z _ < ",
                    color=(1.0,1.0,1.0), # alpha is ignored (set with max_alpha_param)
                    #position=(0,0),
                    font_size=30,
                    #font_name = "cour.ttf",
                    #anchor='left'
                    )
        
        # Create a Viewport instance
        self.viewport = VisionEgg.Core.Viewport(screen=self.screen, stimuli=[self.text])
        
        # Create an instance of the Controller class
        self.target_position_controller = FunctionController(during_go_func=self.get_target_position)
        
        #############################################################
        #  Connect the controllers with the variables they control  #
        #############################################################
        p.add_controller(self.text,'position', self.target_position_controller )
    
    def on_play(self):
        self.main_loop()
    
    def on_pause(self):
        self.paused = not self.paused

    def on_quit(self):
        self.stopping = True
        while not self.stopped:
            self.p = Presentation(go_duration=(10.0,'seconds'),viewports=[self.viewport])
            #######################
            #  Run the stimulus!  #
            #######################

            p.go()
            
            time.sleep(10)
    
    def on_interaction_event(self, data):
        pass

    def on_control_event(self, data):
        pass

    # define position as a function of time
    def get_target_position(t):
        return ( self.max_vel*sin(0.1*2.0*pi*t) + self.mid_x , # x
                 self.max_vel*sin(0.1*2.0*pi*t) + self.mid_y ) # y


if __name__ == "__main__":
    import logging
    logging.basicConfig(level=logging.DEBUG)
    
    fb = FeedbackSkeleton()
    fb.on_init()
    fb.on_play()



